package TUILayer;

import java.util.ArrayList;

import ControlLayer.*;
import ModelLayer.RealException;

public class PersonUI {

	private PersonCtr personCtr;
	private InputParser inputParser;
	private LoginInfo loginInfo;

	public PersonUI(InputParser inputParser, LoginInfo loginInfo) {
		personCtr = new PersonCtr();
		this.inputParser = inputParser;
		this.loginInfo = loginInfo;
	}

	public void start(int menu) throws RealException {
		int choise = 0;
		boolean exit = false;
		switch (loginInfo.getLoggedEmployeePositionId()) {
		case 1: {
			if (menu == 1) {
				while (!exit) {
					choise = inputParser
							.getInt("\f *** CustomerMenu ***\n\n1. Show customer information\n2. Create customer\n3. Update customer\n4. Delete customer\n5. List all customers\n6. Return\n\nMake your choise: ");
					switch (choise) {
					case 1: {
						showCustomer();
						break;
					}
					case 2: {
						createCustomer();
						break;
					}
					case 3: {
						updateCustomer();
						break;
					}
					case 4: {
						deleteCustomer();
						break;
					}
					case 5: {
						listAllCustomers();
						break;
					}
					case 6: {
						exit = true;
						break;
					}
					}
				}
			} else if (menu == 2) {
				while (!exit) {
					choise = inputParser
							.getInt("\f *** ContractorMenu ***\n\n1. Show contractor information\n2. Create contractor\n3. Update contractor\n4. Delete contractor\n5. List all contractors\n6. Return\n\nMake your choise: ");
					switch (choise) {
					case 1: {
						showContractor();
						break;
					}
					case 2: {
						createContractor();
						break;
					}
					case 3: {
						updateContractor();
						break;
					}
					case 4: {
						deleteContractor();
						break;
					}
					case 5: {
						listAllContractors();
						break;
					}
					case 6: {
						exit = true;
						break;
					}
					}
				}
			} else if (menu == 3) {
				while (!exit) {
					choise = inputParser
							.getInt("\f *** EmployeeMenu ***\n\n1. Show employee information\n2. Create employee\n3. Update employee\n4. Delete employee\n5. List all employees\n6. Return\n\nMake your choise: ");
					switch (choise) {
					case 1: {
						showEmployee();
						break;
					}
					case 2: {
						createEmployee();
						break;
					}
					case 3: {
						updateEmployee();
						break;
					}
					case 4: {
						deleteEmployee();
						break;
					}
					case 5: {
						listAllEmployees();
						break;
					}
					case 6: {
						exit = true;
						break;
					}
					}
				}
			}
			break;
		}
		case 2: {
			if (menu == 1) {
				while (!exit) {
					choise = inputParser
							.getInt("\f *** CustomerMenu ***\n\n1. Show customer information\n2. Create customer\n3. Update customer\n4. Delete customer\n5. List all customers\n6. Return\n\nMake your choise: ");
					switch (choise) {
					case 1: {
						showCustomer();
						break;
					}
					case 2: {
						createCustomer();
						break;
					}
					case 3: {
						updateCustomer();
						break;
					}
					case 4: {
						deleteCustomer();
						break;
					}
					case 5: {
						listAllCustomers();
						break;
					}
					case 6: {
						exit = true;
						break;
					}
					}
				}
			} else if (menu == 2) {
				while (!exit) {
					choise = inputParser
							.getInt("\f *** ContractorMenu ***\n\n1. Show contractor information\n2. Create contractor\n3. Update contractor\n4. Delete contractor\n5. List all contractors\n6. Return\n\nMake your choise: ");
					switch (choise) {
					case 1: {
						showContractor();
						break;
					}
					case 2: {
						createContractor();
						break;
					}
					case 3: {
						updateContractor();
						break;
					}
					case 4: {
						deleteContractor();
						break;
					}
					case 5: {
						listAllContractors();
						break;
					}
					case 6: {
						exit = true;
						break;
					}
					}
				}
			} else if (menu == 3) {
				while (!exit) {
					choise = inputParser
							.getInt("\f *** EmployeeMenu ***\n\n1. Show employee information\n2. Create employee\n3. Update employee\n4. Delete employee\n5. List all employees\n6. Return\n\nMake your choise: ");
					switch (choise) {
					case 1: {
						showEmployee();
						break;
					}
					case 2: {
						createEmployee();
						break;
					}
					case 3: {
						updateEmployee();
						break;
					}
					case 4: {
						deleteEmployee();
						break;
					}
					case 5: {
						listAllEmployees();
						break;
					}
					case 6: {
						exit = true;
						break;
					}
					}
				}
			}
			break;
		}
		case 3: {
			while (!exit) {
				choise = inputParser
						.getInt("\f *** CustomerMenu ***\n\n1. Show customer information\n2. Create customer\n3. Update customer\n4. Delete customer\n5. List all customers\n6. Return\n\nMake your choise: ");
				switch (choise) {
				case 1: {
					showCustomer();
					break;
				}
				case 2: {
					createCustomer();
					break;
				}
				case 3: {
					updateCustomer();
					break;
				}
				case 4: {
					deleteCustomer();
					break;
				}
				case 5: {
					listAllCustomers();
					break;
				}
				case 6: {
					exit = true;
					break;
				}
				}
			}
		}
		}
	}

	private void showCustomer() throws RealException {
		int id = inputParser
				.getInt("\f ***ShowCustomerMenu ***\n\nPlease input the id of the customer: ");
		System.out.println("\f ***ShowCustomerMenu ***\n");
		try {
			System.out.println(personCtr.printCustomer(id));
		} catch (RealException e) {
			System.out.println(e.getError());
		}
		inputParser.printReturn();
	}

	private void createCustomer() throws RealException {
		String name = inputParser
				.getString("\f ***CreateCustomerMenu ***\n\nPlease input the name of the new customer: ");
		String address = inputParser
				.getString("\f ***CreateCustomerMenu ***\n\nPlease input the address of the new customer: ");
		long phone = inputParser
				.getLong("\f ***CreateCustomerMenu ***\n\nPlease input the phone number of the new customer: ");
		int group = inputParser
				.getInt("\f ***CreateCustomerMenu ***\n\nPlease input the group id of the new customer: ");
		try {
			personCtr.createCustomer(name, address, phone, group);
		} catch (RealException e) {
			System.out.println("\f ***CreateCustomerMenu ***\n\n"
					+ e.getError());
		}
		inputParser.printReturn();
	}

	public void updateCustomer() throws RealException {
		boolean found = true;
		int id = inputParser
				.getInt("\f ***UpdateCustomerMenu ***\n\nPlease input the id of the customer: ");
		try {
			personCtr.checkCustomer(id);
		} catch (RealException e) {
			found = false;
			System.out.println("\f ***UpdateCustomerMenu ***\n\n"
					+ e.getError());
			inputParser.printReturn();
		}
		if (found) {
			int choise = 0;
			String value = "";
			boolean exit = false;
			while (!exit) {
				choise = inputParser
						.getInt("\f ***UpdateCustomerMenu ***\n\n1. Edit name\n2. Edit address\n3. Edit phone\n4. Edit group\n5. Return\n\nMake your choise: ");
				switch (choise) {
				case 1: {
					value = inputParser
							.getString("\f ***UpdateCustomerMenu ***\n\nPlease input the new name of the customer: ");
					break;
				}
				case 2: {
					value = inputParser
							.getString("\f ***UpdateCustomerMenu ***\n\nPlease input the new address of the customer: ");
					break;
				}
				case 3: {
					value = String
							.valueOf(inputParser
									.getLong("\f ***UpdateCustomerMenu ***\n\nPlease input the new phone of the customer: "));
					break;
				}
				case 4: {
					value = String
							.valueOf(inputParser
									.getInt("\f ***UpdateCustomerMenu ***\n\nPlease input the new group id of the customer: "));
					break;
				}
				case 5: {
					exit = true;
					break;
				}
				}
				if (choise >= 1 && choise <= 4) {
					try {
						personCtr.updateCustomer(id, choise, value);
					} catch (RealException e) {
						System.out.println("\f ***UpdateCustomerMenu ***\n\n"
								+ e.getError());
					}
					inputParser.printReturn();
				}
			}
		}
	}

	public void deleteCustomer() throws RealException {
		int id = inputParser
				.getInt("\f ***DeleteCiustomerMenu ***\n\nPlease input the id of the customer: ");
		try {
			personCtr.deleteContractor(id);
		} catch (RealException e) {
			System.out.println("\f ***DeleteCiustomerMenu ***\n\n"
					+ e.getError());
			inputParser.printReturn();
		}
	}

	public void listAllCustomers() {
		ArrayList<String> prints = personCtr.getAllCustomers();
		System.out.println("\f ***ListAllCustomersMenu ***\n");
		for (String text : prints) {
			System.out.println(text);
		}
		inputParser.printReturn();
	}

	private void showContractor() throws RealException {
		int id = inputParser
				.getInt("\f ***ShowContractorMenu ***\n\nPlease input the id of the contractor: ");
		System.out.println("\f ***ShowContractorMenu ***\n");
		try {
			System.out.println(personCtr.printContractor(id));
		} catch (RealException e) {
			System.out.println("\f ***ShowContractorMenu ***\n\n"
					+ e.getError());
		}
		inputParser.printReturn();
	}

	private void createContractor() throws RealException {
		String name = inputParser
				.getString("\f ***CreateContractorMenu ***\n\nPlease input the name of the new contractor: ");
		String address = inputParser
				.getString("\f ***CreateContractorMenu ***\n\nPlease input the address of the new contractor: ");
		long phone = inputParser
				.getLong("\f ***CreateContractorMenu ***\n\nPlease input the phone number of the new contractor: ");
		personCtr.createContractor(name, address, phone);
		inputParser.printReturn();
	}

	public void updateContractor() throws RealException {
		boolean found = true;
		int id = inputParser
				.getInt("\f ***UpdateContractorMenu ***\n\nPlease input the id of the contractor: ");
		try {
			personCtr.checkContractor(id);
		} catch (RealException e) {
			found = false;
			System.out.println("\f ***UpdateContractorMenu ***\n\n"
					+ e.getError());
			inputParser.printReturn();
		}
		if (found) {
			int choise = 0;
			String value = "";
			boolean exit = false;
			while (!exit) {
				choise = inputParser
						.getInt("\f ***UpdateContractorMenu ***\n\n1. Edit name\n2. Edit address\n3. Edit phone\n4. Return\n\nMake your choise: ");
				switch (choise) {
				case 1: {
					value = inputParser
							.getString("\f ***UpdateContractorMenu ***\n\nPlease input the new name of the customer: ");
					break;
				}
				case 2: {
					value = inputParser
							.getString("\f ***UpdateContractorMenu ***\n\nPlease input the new address of the customer: ");
					break;
				}
				case 3: {
					value = String
							.valueOf(inputParser
									.getLong("\f ***UpdateContractorMenu ***\n\nPlease input the new phone of the customer: "));
					break;
				}
				case 4: {
					exit = true;
					break;
				}
				}
				if (choise >= 1 && choise <= 3) {
					try {
						personCtr.updateContractor(id, choise, value);
					} catch (RealException e) {
						System.out.println("\f ***UpdateContractorMenu ***\n\n"
								+ e.getError());
					}
					inputParser.printReturn();
				}
			}
		}
	}

	public void deleteContractor() throws RealException {
		int id = inputParser
				.getInt("\f ***DeleteContractorMenu ***\n\nPlease input the id of the contractor: ");
		try {
			personCtr.deleteContractor(id);
		} catch (RealException e) {
			System.out.println("\f ***DeleteContractorMenu ***\n\n"
					+ e.getError());
			inputParser.printReturn();
		}
	}

	public void listAllContractors() {
		ArrayList<String> prints = personCtr.getAllContractors();
		System.out.println("\f ***ListAllContractorsMenu ***\n");
		for (String text : prints) {
			System.out.println(text);
		}
		inputParser.printReturn();
	}

	private void showEmployee() throws RealException {
		int id = inputParser
				.getInt("\f ***ShowEmployeeMenu ***\n\nPlease input the id of the employee: ");
		System.out.println("\f ***ShowEmployeeMenu ***\n");
		try {
			System.out.println(personCtr.printEmployee(id));
		} catch (RealException e) {
			System.out.println(e.getError());
		}
		inputParser.printReturn();
	}

	private void createEmployee() throws RealException {
		String name = inputParser
				.getString("\f ***CreateEmployeeMenu ***\n\nPlease input the name of the new employee: ");
		String address = inputParser
				.getString("\f ***CreateEmployeeMenu ***\n\nPlease input the address of the new employee: ");
		long phone = inputParser
				.getLong("\f ***CreateEmployeeMenu ***\n\nPlease input the phone number of the new employee: ");
		String password = inputParser
				.getString("\f ***CreateEmployeeMenu ***\n\nPlease input the password of the new employee: ");
		String position = "";
		boolean exiter = false;
		while(!exiter) {
			int choice = inputParser
					.getInt("\f ***CreateEmployeeMenu ***\n\nPosition of the new employee: \n 1. Admin\n 2. Manager\n 3. Salesman\n 4. Other");
			switch (choice) {
			case 1:
				position = "admin";
				break;
			case 2:
				position = "manager";
				break;
			case 3:
				position = "salesman";
				break;
			case 4:
				position = inputParser
						.getString("\f ***CreateEmployeeMenu ***\n\nPlease input the position of the new employee: ");
				break;
			}
			if (choice >= 1 && choice <= 4) {
				exiter = true;
				personCtr.createEmployee(name, address, phone, password,
						position);
				inputParser.printReturn();
			}
		}
	}

	public void updateEmployee() throws RealException {
		boolean found = true;
		int id = inputParser
				.getInt("\f ***UpdateEmployeeMenu ***\n\nPlease input the id of the employee: ");
		try {
			personCtr.checkEmployee(id);
		} catch (RealException e) {
			found = false;
			System.out.println("\f ***UpdateEmployeeMenu ***\n\n"
					+ e.getError());
			inputParser.printReturn();
		}
		if (found) {
			int choise = 0;
			String value = "";
			boolean exit = false;
			while (!exit) {
				choise = inputParser
						.getInt("\f ***UpdateEmployeeMenu ***\n\n1. Edit name\n2. Edit address\n3. Edit phone\n4. Edit password\n5. Edit position\n6. Return\n\nMake your choise: ");
				switch (choise) {
				case 1: {
					value = inputParser
							.getString("\f ***UpdateEmployeeMenu ***\n\nPlease input the new name of the employee: ");
					break;
				}
				case 2: {
					value = inputParser
							.getString("\f ***UpdateEmployeeMenu ***\n\nPlease input the new address of the cemployee: ");
					break;
				}
				case 3: {
					value = String
							.valueOf(inputParser
									.getLong("\f ***UpdateEmployeeMenu ***\n\nPlease input the new phone of the employee: "));
					break;
				}
				case 4: {
					value = inputParser
							.getString("\f ***UpdateEmployeeMenu ***\n\nPlease input the new password of the employee: ");
					break;
				}
				case 5: {
					boolean exiter = false;
					while(!exiter) {
						int choice = inputParser
								.getInt("\f ***CreateEmployeeMenu ***\n\nPosition of the new employee: \n 1. Admin\n 2. Manager\n 3. Salesman\n 4. Other");
						switch (choice) {
						case 1:
							value = "admin";
							break;
						case 2:
							value = "manager";
							break;
						case 3:
							value = "salesman";
							break;
						case 4:
							value = inputParser
									.getString("\f ***CreateEmployeeMenu ***\n\nPlease input the position of the new employee: ");
							break;
						}
						if (choice >= 1 && choice <= 4) {
							exiter = true;
						}
					}
					break;
				}
				case 6: {
					exit = true;
					break;
				}
				}
				if (choise >= 1 && choise <= 5) {
					try {
						personCtr.updateEmployee(id, choise, value);
					} catch (RealException e) {
						System.out.println("\f ***UpdateEmployeeMenu ***\n\n"
								+ e.getError());
					}
					inputParser.printReturn();
				}
			}
		}
	}

	public void deleteEmployee() throws RealException {
		int id = inputParser
				.getInt("\f ***DeleteEmployeeMenu ***\n\nPlease input the id of the employee: ");
		try {
			personCtr.deleteEmployee(id);
		} catch (RealException e) {
			System.out.println("\f ***DeleteEmployeeMenu ***\n\n"
					+ e.getError());
			inputParser.printReturn();
		}
	}

	public void listAllEmployees() {
		ArrayList<String> prints = personCtr.getAllEmployees();
		System.out.println("\f ***ListAllEmployeesMenu ***\n");
		for (String text : prints) {
			System.out.println(text);
		}
		inputParser.printReturn();
	}
}
